//
//  MYWallet.m
//  reSearchDemo
//
//  Created by 云库 on 15/7/20.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MYWallet.h"
#import "WalletVC.h"
#import "YunDetailVC.h"
#import "MenuView.h"
#import "CommonProblemVC.h"
#import "MYWalletCell.h"

#define fDeviceWidth ([UIScreen mainScreen].bounds.size.width)
#define fDeviceHeight ([UIScreen mainScreen].bounds.size.height-StatusBarHeight)
@interface MYWallet ()
{
    UIView *_headView;
    NSMutableArray *dataource; //*<数据源*/
}
@property(nonatomic,retain)MenuView * menuView;
@property(nonatomic,strong)UICollectionView *collectionView;
@end

@implementation MYWallet

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"";
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menu)];
    self.title=[NSString stringWithFormat:@"%@\n%@",@"我的钱包",@"云库安全支付"];

    self.view.backgroundColor=[UIColor grayColor];
    
//    [self creatView];
    float AD_height = 120;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, AD_height);//头部
    flowLayout.minimumInteritemSpacing = 0.0;
    flowLayout.minimumLineSpacing = 0.0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:self.collectionView];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册cell和ReusableView（相当于头部）
    [self.collectionView registerClass:[MYWalletCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
    
    _headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 120)];
    _headView.backgroundColor=[UIColor grayColor];
    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(_headView.frame.size.width/4-30, 30, 60, 30)];
    lab.backgroundColor=[UIColor grayColor];
    lab.textAlignment=NSTextAlignmentCenter;
    lab.text=@"¥443";
    lab.textColor=[UIColor whiteColor];
    [_headView addSubview:lab];
    UILabel * lab1=[[UILabel alloc]initWithFrame:CGRectMake(_headView.frame.size.width/4-30, 60, 60, 30)];
    lab1.backgroundColor=[UIColor grayColor];
    lab1.textAlignment=NSTextAlignmentCenter;
    lab1.text=@"钱包";
    lab1.textColor=[UIColor whiteColor];
    [_headView addSubview:lab1];
    UILabel * lab2=[[UILabel alloc]initWithFrame:CGRectMake(_headView.frame.size.width/4-50, 90, 100, 30)];
    lab2.backgroundColor=[UIColor grayColor];
    lab2.textAlignment=NSTextAlignmentCenter;
    lab2.text=@"可用金额:2.00";
    lab2.font=[UIFont systemFontOfSize:14];
    lab2.textColor=[UIColor whiteColor];
    [_headView addSubview:lab2];
    UIButton * leftBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame=CGRectMake(0, 0, _headView.frame.size.width/2, _headView.frame.size.height);
    [leftBtn addTarget:self action:@selector(leftbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:leftBtn];
    
    UILabel * lab3=[[UILabel alloc]initWithFrame:CGRectMake(_headView.frame.size.width-120, 30, 60, 30)];
    lab3.backgroundColor=[UIColor grayColor];
    lab3.textAlignment=NSTextAlignmentCenter;
    lab3.text=@"10";
    lab3.textColor=[UIColor whiteColor];
    [_headView addSubview:lab3];
    UILabel * lab4=[[UILabel alloc]initWithFrame:CGRectMake(_headView.frame.size.width-120, 60, 60, 30)];
    lab4.backgroundColor=[UIColor grayColor];
    lab4.textAlignment=NSTextAlignmentCenter;
    lab4.text=@"云币";
    lab4.textColor=[UIColor whiteColor];
    [_headView addSubview:lab4];
    UIButton * rightBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame=CGRectMake(_headView.frame.size.width/2, 0, _headView.frame.size.width/2, _headView.frame.size.height);
    [rightBtn addTarget:self action:@selector(rightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:rightBtn];
    
    dataource = [NSMutableArray arrayWithObjects:@"积分",@"抵用券",@"中间券",@"优惠促销",@"会员卡",@"储值卡", nil];
}
-(void)menu{
    NSArray * arr = nil;
//    if ([share.uid isEqualToString:[BSEngine currentUserId]]) {
//        arr = @[@"发送给朋友", @"保存到手机", @"收藏", @"删除"];
//    } else {
        arr = @[@"交易记录", @"支付管理", @"支付安全", @"常见问题"];
//    }
    _menuView = [[MenuView alloc] initWithButtonTitles:arr withDelegate:self];
    _menuView.hasImage = NO;
    [_menuView showInView:self.view origin:CGPointMake(self.view.width - 180, 70)];
    _menuView.buttonView.delegate=self;
}
-(void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_menuView hide];
    CommonProblemVC * vc=[[CommonProblemVC alloc]init];
    if (indexPath.row==3) {
        [self pushViewController:vc];

    }
    
}
/*
-(void)creatView{
    UIView * view=[[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.width, 120)];
    view.backgroundColor=[UIColor grayColor];
    [self.view addSubview:view];
    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width/4-30, 30, 60, 30)];
    lab.backgroundColor=[UIColor grayColor];
    lab.textAlignment=NSTextAlignmentCenter;
    lab.text=@"¥443";
    lab.textColor=[UIColor whiteColor];
    [view addSubview:lab];
    UILabel * lab1=[[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width/4-30, 60, 60, 30)];
    lab1.backgroundColor=[UIColor grayColor];
    lab1.textAlignment=NSTextAlignmentCenter;
    lab1.text=@"钱包";
    lab1.textColor=[UIColor whiteColor];
    [view addSubview:lab1];
    UILabel * lab2=[[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width/4-50, 90, 100, 30)];
    lab2.backgroundColor=[UIColor grayColor];
    lab2.textAlignment=NSTextAlignmentCenter;
    lab2.text=@"可用金额:2.00";
    lab2.font=[UIFont systemFontOfSize:14];
    lab2.textColor=[UIColor whiteColor];
    [view addSubview:lab2];
    UIButton * leftBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame=CGRectMake(0, 0, view.frame.size.width/2, view.frame.size.height);
    [leftBtn addTarget:self action:@selector(leftbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftBtn];
    
    UILabel * lab3=[[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width-120, 30, 60, 30)];
    lab3.backgroundColor=[UIColor grayColor];
    lab3.textAlignment=NSTextAlignmentCenter;
    lab3.text=@"10";
    lab3.textColor=[UIColor whiteColor];
    [view addSubview:lab3];
    UILabel * lab4=[[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width-120, 60, 60, 30)];
    lab4.backgroundColor=[UIColor grayColor];
    lab4.textAlignment=NSTextAlignmentCenter;
    lab4.text=@"云币";
    lab4.textColor=[UIColor whiteColor];
    [view addSubview:lab4];
    UIButton * rightBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame=CGRectMake(view.frame.size.width/2, 0, view.frame.size.width/2, view.frame.size.height);
    [rightBtn addTarget:self action:@selector(rightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:rightBtn];
    int totalloc=3;
        CGFloat appvieww=view.frame.size.width/3;
        CGFloat appviewh=(self.view.frame.size.height-view.frame.origin.y-view.frame.size.height)/2;
    
         CGFloat margin=2;
    NSArray * arr=[[NSArray alloc]initWithObjects:@"积分", @"抵用劵",@"中奖劵",@"优惠促销",@"会员卡",@"银行卡",nil];
         for (int i=0; i<6; i++) {
                 int row=i/totalloc;//行号
                 //1/3=0,2/3=0,3/3=1;
                 int loc=i%totalloc;//列号
       
                 CGFloat appviewx=margin+(margin+appvieww)*loc;
                 CGFloat appviewy=margin+(margin+appviewh)*row;
        
        
                 //创建uiview控件
             UIButton *appview=[UIButton buttonWithType:UIButtonTypeSystem];
             appview.frame=CGRectMake(appviewx, appviewy+view.frame.origin.y+view.frame.size.height, appvieww, appviewh);
                 [appview setBackgroundColor:[UIColor whiteColor]];
             [appview setTitle:[NSString stringWithFormat:@"%@",arr[i]] forState:UIControlStateNormal];
             appview.tintColor=[UIColor blackColor];
                 [self.view addSubview:appview];
         }
}
 */
-(void)leftbtnClick:(UIButton*)sender{
  //  DLog(@"leftbtnClick");
    WalletVC * vc=[[WalletVC alloc]init];
    [self pushViewController:vc];
}
-(void)rightBtn:(UIButton*)btn{
    YunDetailVC * vc=[[YunDetailVC alloc]init];
    [self pushViewController:vc];
}

#pragma mark -
#pragma mark --UIcolletionViewDelegate&dataSource--
//定义展示的UIcolletionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return dataource.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
//每个UIcolletionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identify = @"cell";
    MYWalletCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    [cell sizeToFit];
    if (!cell) {
        DLog(@"无法创建CollectionViewCell时打印，自定义的cell就不可能进来了。");
    }
    cell.imgView.image = [UIImage imageNamed:@"云平台.png"];
    cell.text.text = dataource[indexPath.row];
    cell.Number.text = @"0";
    
    return cell;
}
//头部显示的内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    
    [headerView addSubview:_headView];//头部广告栏
    return headerView;

}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //边距占5*4=20 ，2个
    //图片为正方形，边长：(fDeviceWidth-20)/2-5-5 所以总高(fDeviceWidth-20)/2-5-5 +20+30+5+5 label高20 btn高30 边
    return CGSizeMake((fDeviceWidth)/3, (fDeviceWidth)/3);
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //    cell.backgroundColor = [UIColor redColor];
    DLog(@"选择%ld",indexPath.row);
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
